﻿/// <summary>
/// 时间：2020.4.23
/// 作用：处理并输出用户的输入
/// 更新：NULL
/// </summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CommonLib
{
	public class InputControl : MonoBehaviour
	{
		[SerializeField]
		float defaultDoubleClickDuration = 0.25f;
		protected Dictionary<string, KeyCodeListener> m_ListenerDic = new Dictionary<string, KeyCodeListener>();

		private void Awake()
		{
			InitListener();
		}

		private void Update()
		{
			IEnumerator _value =  m_ListenerDic.Values.GetEnumerator();
			while (_value.MoveNext())
			{
				KeyCodeListener lis = _value.Current as KeyCodeListener;
				lis.UpdateListenerState();
			}		
		}

		//初始化所有按键监听器
		protected virtual void InitListener()
		{
			m_ListenerDic.Add("W", new KeyCodeListener() { listenKey = "w" , doubleClickDuration = defaultDoubleClickDuration });
			m_ListenerDic.Add("A", new KeyCodeListener() { listenKey = "a" , doubleClickDuration = defaultDoubleClickDuration });
			m_ListenerDic.Add("S", new KeyCodeListener() { listenKey = "s" , doubleClickDuration = defaultDoubleClickDuration });
			m_ListenerDic.Add("D", new KeyCodeListener() { listenKey = "d" , doubleClickDuration = defaultDoubleClickDuration });
		}

		public virtual bool GetKeyState(string keyName, KeyState state)
		{
			switch (state)
			{
				case KeyState.Click:
					return GetKeyIsClick(keyName);
				case KeyState.Press:
					return GetKeyIsPress(keyName);
				case KeyState.DoubleClick:
					return GetKeyIsDoubleClick(keyName);
				default:
					return false;
			}
		}

		public virtual bool GetKeyIsPress(string keyName)
		{
			if( m_ListenerDic.TryGetValue(keyName, out KeyCodeListener listener))
			{
				return listener.IsPress;
			}
			else
			{
				/*
				KeyCodeListener lis = new KeyCodeListener()
				{
					listenKey = keyName,
					doubleClickDuration = defaultDoubleClickDuration
				};
				m_ListenerDic.Add(keyName, lis);
				lis.UpdateListenerState();
				*/
				return AddKeyCodeListener(keyName).IsPress;
			}
		}

		public virtual bool GetKeyIsClick(string keyName)
		{
			if (m_ListenerDic.TryGetValue(keyName, out KeyCodeListener listener))
			{
				return listener.IsClick;
			}
			else
			{
				return AddKeyCodeListener(keyName).IsClick;
			}
		}

		public virtual bool GetKeyIsDoubleClick(string keyName)
		{
			if (m_ListenerDic.TryGetValue(keyName, out KeyCodeListener listener))
			{
				return listener.IsDoubleClick;
			}
			else
			{
				return AddKeyCodeListener(keyName).IsDoubleClick;
			}
		}

		KeyCodeListener AddKeyCodeListener(string keyName)
		{
			KeyCodeListener lis = new KeyCodeListener()
			{
				listenKey = keyName,
				doubleClickDuration = defaultDoubleClickDuration
			};
			m_ListenerDic.Add(keyName, lis);
			lis.UpdateListenerState();
			return lis;
		}
	}

	public class KeyCodeListener
	{
		public string listenKey;

		public bool IsPress { get; private set; }
		public bool IsClick { get; private set; }
		public bool IsDoubleClick { get; private set; }

		public float doubleClickDuration;
		float clickTime = 0;

		public void UpdateListenerState()
		{
			if( null != listenKey)
			{
				CheckPress();
				CheckClick();
				CheckDoubleClick();
			}
		}

		#region 更新自身按压状态
		virtual protected void CheckPress()
		{ 
			IsPress = Input.GetKey(listenKey);
		}
		#endregion
		#region 更新自身点击状态
		virtual protected void CheckClick()
		{
			IsClick = Input.GetKeyDown(listenKey);
		}
		#endregion
		#region 更新自身双击状态
		virtual protected void CheckDoubleClick()
		{
			if (Input.GetKeyDown(listenKey))
			{
				float curTime = Time.fixedTime;
				if(curTime - clickTime <= doubleClickDuration)
				{
					IsDoubleClick = true;
					//Debug.Log("DoubelClick");
					//Reset firstClick
					clickTime = 0;
				}
				else
				{
					clickTime = curTime;
				}
			}
		}
		#endregion
	}
	public enum KeyState
	{
		None = 0,
		Click = 1,
		Press = 2,
		DoubleClick = 3,
	}
}